package com.ocom.payconfig.controller;

import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.dto.MealMenuBDTO;
import com.ocom.common.entity.payconfig.EntityDishesPrice;
import com.ocom.common.entity.payconfig.EntityMealMenu;
import com.ocom.common.entity.payconfig.EntityMealPlan;
import com.ocom.common.request.payconfig.GetChangeDishesListRequest;
import com.ocom.common.request.payconfig.MealPlanInfoRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.payconfig.DayListMealListDishesListVo;
import com.ocom.common.vo.payconfig.DayListMealListVo;
import com.ocom.common.vo.payconfig.DayListVo;
import com.ocom.payconfig.service.DishesPriceService;
import com.ocom.payconfig.service.MealMenuService;
import com.ocom.payconfig.service.MealPlanService;
import com.ocom.payconfig.service.ReceiptAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2023/2/20
 **/
@Slf4j
@RestController
public class MealMeunController {

	@Autowired
	private MealMenuService mealMenuService;

	@Autowired
	private DishesPriceService dishesPriceService;

	@Autowired
	private ReceiptAddressService receiptAddressService;

	@Autowired
	MealPlanService mealPlanService;

	//11.3.5餐段菜单列表（mealMenuList）
	@PostMapping(PayConfigUrl.Url_mealMenuList)
	public Result mealMenuList(@RequestBody EntityMealMenu entityMealMenu) {

		EntityMealPlan entityMealPlan = mealPlanService.mealPlanInfo(entityMealMenu.getComId(), entityMealMenu.getMealplanId());

		entityMealMenu.setMerchId(entityMealPlan.getMerchId());
		entityMealMenu.setPointId(entityMealPlan.getPointId());

		List<EntityDishesPrice> priceList = dishesPriceService.getDishesPrice(entityMealMenu.getComId(), entityMealMenu.getMerchId(), entityMealMenu.getPointId());

		List<EntityMealMenu> mealMenuList = mealMenuService.mealMenuList(entityMealMenu.getComId(), entityMealMenu);

		if (mealMenuList == null || mealMenuList.size() == 0) {
			return ResultUtil.success(new ArrayList<>());
		}
		List<DayListVo> dayListVos = new ArrayList<>();

		List<Integer> daysIn = new ArrayList<>();
		for (EntityMealMenu mealMenu : mealMenuList) {
//			List<Integer> daysIn = mealMenuList.stream().map(EntityMealMenu -> EntityMealMenu.getDays()).collect(Collectors.toList());
			//天不存在  添加新数据
			if (!daysIn.contains(mealMenu.getDays())) {
				daysIn.add(mealMenu.getDays());
				DayListVo dayListVo = new DayListVo();
				dayListVo.setDays(mealMenu.getDays());

				DayListMealListVo dayListMealListVo = new DayListMealListVo();
				dayListMealListVo.setMealNo(mealMenu.getMealNo());

				DayListMealListDishesListVo dayListMealListDishesListVo = new DayListMealListDishesListVo();
				dayListMealListDishesListVo.setDishesId(mealMenu.getDishesId());
				dayListMealListDishesListVo.setDishesName(mealMenu.getDishesName());
				dayListMealListDishesListVo.setDishesClassId(mealMenu.getDishesClassId());
				dayListMealListDishesListVo.setSort(mealMenu.getSort());
				dayListMealListDishesListVo.setPriceMode(mealMenu.getPriceMode());
				dayListMealListDishesListVo.setMerchId(mealMenu.getMerchId());
				dayListMealListDishesListVo.setLimitedQuantity(mealMenu.getLimitedQuantity());

				if (mealMenu.getPrice() >= 0) {
					dayListMealListDishesListVo.setSpecial(1);
					dayListMealListDishesListVo.setPrice(mealMenu.getPrice());
				} else {
					if (priceList == null || priceList.size() == 0) {
						dayListMealListDishesListVo.setSpecial(0);
						dayListMealListDishesListVo.setPrice(0);
					} else {
						for (EntityDishesPrice entityDishesPrice : priceList) {
							if (entityDishesPrice.getDishesId().equals(mealMenu.getDishesId()) && Integer.valueOf(entityDishesPrice.getPriceMode()).equals(mealMenu.getPriceMode())) {
								dayListMealListDishesListVo.setSpecial(0);
								dayListMealListDishesListVo.setPrice(entityDishesPrice.getPrice());
								break;
							}
						}
					}
				}
				dayListMealListVo.getDishesList().add(dayListMealListDishesListVo);
				dayListVo.getMealList().add(dayListMealListVo);
				dayListVos.add(dayListVo);
			} else {
				//天已存在
				for (int i = 0; i < dayListVos.size(); i++) {
					if (mealMenu.getDays().equals(dayListVos.get(i).getDays())) {
						//最外层
						DayListVo dayListVo = dayListVos.get(i);

						List<DayListMealListVo> mealList = dayListVo.getMealList();

						List<Integer> mealIn = mealList.stream().map(DayListMealListVo -> DayListMealListVo.getMealNo()).collect(Collectors.toList());

						//餐段不存在
						if (!mealIn.contains(mealMenu.getMealNo())) {
							DayListMealListVo dayListMealListVo = new DayListMealListVo();
							dayListMealListVo.setMealNo(mealMenu.getMealNo());
							DayListMealListDishesListVo dayListMealListDishesListVo = new DayListMealListDishesListVo();
							dayListMealListDishesListVo.setDishesId(mealMenu.getDishesId());
							dayListMealListDishesListVo.setDishesName(mealMenu.getDishesName());
							dayListMealListDishesListVo.setDishesClassId(mealMenu.getDishesClassId());
							dayListMealListDishesListVo.setSort(mealMenu.getSort());
							dayListMealListDishesListVo.setPriceMode(mealMenu.getPriceMode());
							dayListMealListDishesListVo.setLimitedQuantity(mealMenu.getLimitedQuantity());
							dayListMealListDishesListVo.setMerchId(mealMenu.getMerchId());
							if (mealMenu.getPrice() >= 0) {
								dayListMealListDishesListVo.setSpecial(1);
								dayListMealListDishesListVo.setPrice(mealMenu.getPrice());
							} else {
								if (priceList == null || priceList.size() == 0) {
									dayListMealListDishesListVo.setSpecial(0);
									dayListMealListDishesListVo.setPrice(mealMenu.getPrice());
								} else {
									for (EntityDishesPrice entityDishesPrice : priceList) {
										if (entityDishesPrice.getDishesId().equals(mealMenu.getDishesId()) && Integer.valueOf(entityDishesPrice.getPriceMode()).equals(mealMenu.getPriceMode())) {
											dayListMealListDishesListVo.setSpecial(0);
											dayListMealListDishesListVo.setPrice(entityDishesPrice.getPrice());
											break;
										}
									}
								}
							}

							dayListMealListVo.getDishesList().add(dayListMealListDishesListVo);
							mealList.add(dayListMealListVo);
							dayListVo.setMealList(mealList);
							dayListVos.set(i, dayListVo);
						} else {
							for (int j = 0; j < mealList.size(); j++) {
								if (mealMenu.getMealNo().equals(mealList.get(j).getMealNo())) {
									DayListMealListVo dayListMealListVo = mealList.get(j);

									DayListMealListDishesListVo dayListMealListDishesListVo = new DayListMealListDishesListVo();
									dayListMealListDishesListVo.setDishesId(mealMenu.getDishesId());
									dayListMealListDishesListVo.setDishesName(mealMenu.getDishesName());
									dayListMealListDishesListVo.setDishesClassId(mealMenu.getDishesClassId());
									dayListMealListDishesListVo.setSort(mealMenu.getSort());
									dayListMealListDishesListVo.setPriceMode(mealMenu.getPriceMode());
									dayListMealListDishesListVo.setLimitedQuantity(mealMenu.getLimitedQuantity());
									dayListMealListDishesListVo.setMerchId(mealMenu.getMerchId());
									if (mealMenu.getPrice() >= 0) {
										dayListMealListDishesListVo.setSpecial(0);
										dayListMealListDishesListVo.setPrice(mealMenu.getPrice());
									} else {
										if (priceList == null || priceList.size() == 0) continue;
										for (EntityDishesPrice entityDishesPrice : priceList) {
											if (entityDishesPrice.getDishesId().equals(mealMenu.getDishesId()) && Integer.valueOf(entityDishesPrice.getPriceMode()).equals(mealMenu.getPriceMode())) {
												dayListMealListDishesListVo.setSpecial(0);
												dayListMealListDishesListVo.setPrice(entityDishesPrice.getPrice());
												break;
											}
										}
									}

									dayListMealListVo.getDishesList().add(dayListMealListDishesListVo);
									mealList.set(j, dayListMealListVo);
									dayListVo.setMealList(mealList);
									dayListVos.set(i, dayListVo);

									break;
								}
							}
						}
						break;
					}
				}
			}
		}
		Map<String, Object> objectMap = new HashMap<>();
		objectMap.put("dayList", dayListVos);

		return ResultUtil.success(objectMap);
	}

	//11.3.6清除餐段菜单（clearMealMenu）
	@PostMapping(PayConfigUrl.Url_clearMealMenu)
	public Result clearMealMenu(@RequestBody EntityMealMenu entityMealMenu) {
		mealMenuService.clearMealMenu(entityMealMenu.getComId(), entityMealMenu);
		return ResultUtil.success();
	}

	//11.3.7保存餐段菜单（saveMealMenu）
	@PostMapping(PayConfigUrl.Url_saveMealMenu)
	public Result saveMealMenu(@RequestBody EntityMealMenu entityMealMenu) {
		mealMenuService.saveMealMenu(entityMealMenu.getComId(), entityMealMenu);
		return ResultUtil.success();
	}

	//11.3.8删除餐段菜单（removeMealMenu）
	@PostMapping(PayConfigUrl.Url_removeMealMenu)
	public Result removeMealMenu(@RequestBody EntityMealMenu entityMealMenu) {
		mealMenuService.removeMealMenu(entityMealMenu.getComId(), entityMealMenu);
		return ResultUtil.success();
	}


	//11.3.9排餐计划详情（ealPlanInfo）
	@PostMapping(PayConfigUrl.Url_mealPlanInfo)
	public Result mealPlanInfo(@RequestBody MealPlanInfoRequest request) {
		EntityMealPlan entityMealPlan = mealPlanService.mealPlanInfo(request.getComId(), request.getMealplanId());

		Map<String, Object> objectMap = new HashMap<>();
		objectMap.put("mealPlanInfo", entityMealPlan);
		try {
			if (CommonUtil.isNull(entityMealPlan.getReceiptIds())) {
				objectMap.put("receiptAddress", receiptAddressService.getReceiptInIds(request.getComId(), null));
			} else {
				List<String> industryTypeStr = Arrays.asList(entityMealPlan.getReceiptIds().split(","));
				List<Integer> industryTypeInt = industryTypeStr.stream().map(Integer::valueOf).collect(Collectors.toList());
				objectMap.put("receiptAddress", receiptAddressService.getReceiptInIds(request.getComId(), industryTypeInt));
			}
		} catch (Exception exception) {
			log.info("获取取餐点列表报错");
		}
		return ResultUtil.success(objectMap);

	}

	/**
	 * 11.3.11餐段菜单列表（mealMenuListB）
	 *
	 * @param mealMenuBDTO
	 * @return
	 * @throws Exception
	 */
	@PostMapping(PayConfigUrl.Url_SaveMealMenuB)
	public Result<Boolean> saveMealMenuB(@RequestBody MealMenuBDTO mealMenuBDTO) throws Exception {
		return ResultUtil.success(mealMenuService.mealMenuService(mealMenuBDTO));
	}

	/**
	 * 11.3.11餐段菜单列表（mealMenuListB）
	 *
	 * @param comId
	 * @param mealPlanId
	 * @return
	 * @throws Exception
	 */
	@GetMapping(PayConfigUrl.Url_MealMenuListB)
	public Result<Map<String, Object>> mealMenuListB(@RequestParam Long comId, @RequestParam Integer mealPlanId) throws Exception {
		return mealMenuService.mealMenuListB(comId, mealPlanId);
	}

	@PostMapping(PayConfigUrl.URL_getChangeDishesList)
	public Result getChangeDishesList(@RequestBody GetChangeDishesListRequest request){
		return this.dishesPriceService.getChangeDishesList(request.getComId(),request);
	}
}
